#include "threadbase.h"
#include <QThread>
#include <QtDebug>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QMutex>
#include "src/Utils/dbconnhelper.h"
#include "src/Utils/inifilehelper.h"
#include "src/Entity/clientdefect.h"

ClientDefectThread::ClientDefectThread(int seq,SteelInfo *s)
    :sequences(seq),steel(s)
{
    QString connName = QString("Conn_ClientDefect");
    QString TabName = QString("ClientDefectDB1");
    db = dbhelper.openDataBase(connName,TabName);
}


QList<int> getWidthFile(QString Sequnence, int CamNo, int imgIndex){
    int LL = 0,RR = 0,Len = 0;
    if(CamNo <= 2){
        auto Steel_left_List =readFile(QString(width1).arg(Sequnence),imgIndex);
        auto Steel_right_List = readFile(QString(width2).arg(Sequnence),imgIndex);
        if(Steel_left_List.size() > 0 && Steel_right_List.size() > 0){
            LL = Steel_left_List[1];
            RR = Steel_right_List[2];
            if(CamNo == 1 ){
                Len = Steel_left_List[0];
            }else{
                Len = Steel_right_List[0];
            }
        }

    }
    else
    {
        auto Steel_left_List = readFile(QString(width3).arg(Sequnence),imgIndex);
        auto Steel_right_List= readFile(QString(width4).arg(Sequnence),imgIndex);
        if(Steel_left_List.size() > 0 && Steel_right_List.size() > 0){
            LL = Steel_left_List[1];
            RR = Steel_right_List[2];
            if(CamNo == 3  ){
                Len = Steel_left_List[0];
            }else{
                Len = Steel_right_List[0];
            }
        }

    }
    return { LL,RR,Len };
}

void ClientDefectThread::run()
{
    if( sequences<=0){
        return;
    }

    qDebug()<<"当前检测的相机数量:"<<IniFilehelper::camNum;

    for (int cameraIndex = 1; cameraIndex <= IniFilehelper::camNum; cameraIndex++) {
    QString str =R"(
        USE ClientDefectDB%1
        SELECT steel.SteelID ,defect.CameraNo ,defect.DefectNo,defect.Class,config.Name,
        abs(defect.LeftInSteel- defect.RightInSteel) as width,
        abs(defect.TopInSteel - defect.BottomInSteel) as height,defect.Area ,
        defect.TopInSteel,
        defect.BottomInSteel,
        defect.LeftInSteel,
        defect.RightInSteel,
        defect.ImageIndex,
        steel.TopLen
        FROM
        [dbo].[Defect] as defect,
        [SteelRecord].[dbo].[steel] as steel,
        [SteelRecord].[dbo].[SteelID] as steelid,
        ConfigCenter.[dbo].[DefectClass] as config
        WHERE
        defect.SteelNo = steel.SequeceNo
        and config.Class = defect.Class
        and steel.SteelID =steelid.ID
        %3
        and defect.SteelNo = %2;
                    )";
        QString sqlStr =  str.arg(cameraIndex).arg(sequences).arg( IniFilehelper::ignoreMeter > 0 ? QString(" and defect.TopInSteel >= %1 ").arg(IniFilehelper::ignoreMeter) : "");
        QSqlQuery query(db);

        query.exec(sqlStr);
        while(query.next()){
            steel->mutex.lock();
            int cno = query.record().value("CameraNo").toInt();
            int dno = query.record().value("DefectNo").toInt();
            int clz = query.record().value("Class").toInt();
            QString DefectName = query.record().value("Name").toString();
            int w = query.record().value("width").toInt();
            int h = query.record().value("height").toInt();
            int a = query.record().value("Area").toInt();
            int l = query.record().value("LeftInSteel").toInt();
            int r = query.record().value("RightInSteel").toInt();
            int t = query.record().value("TopInSteel").toInt();
            int imgIdx = query.record().value("ImageIndex").toInt();
            int toplen = query.record().value("TopLen").toInt();
            int topInSteel = t;
            int bottomInSteel = toplen - t;


            QString seq = QString("%1").arg(sequences,6,10,QLatin1Char('0'));

            QList<int> widthList = getWidthFile(seq,cno,imgIdx);
            int leftInSteel = qAbs(widthList[0]) + l + (w/2);
            int rightInSteel = widthList[1] + qAbs(r) + (w/2);


            steel->DefectNameList.insert(DefectName);
            steel->ClientDefect1ist.append(new ClientDefect(cno,dno,w,h,a,clz,DefectName,leftInSteel,rightInSteel,bottomInSteel,topInSteel,imgIdx,0));
            steel->mutex.unlock();
        }
    }

    emit ClientDefectFinish(steel);

}

//------------------------------------------------------------

SteelThread::SteelThread(int seq, SteelInfo *s)
    :sequences(seq),steel(s)
{
   db = dbhelper.openDataBase("conn_Steel","SteelRecord");
}

void SteelThread::run()
{

    QString str = R"(
            use SteelRecord
            select steel.SteelID,
                steelid.Width,
                steelid.Thick,
                steelid.Length,
                steelid.SteelType,
                steel.TopLen
            from [SteelRecord].[dbo].[steel] as steel,
                 [SteelRecord].[dbo].[SteelID] as steelid
            where
                steel.SteelID = steelid.ID
                and steel.[SequeceNo] = %1 ;
        )";

    QString sqlStr = str.arg(sequences);
        if(sequences > 0){
            QSqlQuery query(db);
            query.exec(sqlStr);
            while (query.next()) {
                steel->setSteelNo(sequences);
                QString steelID = query.record().value("SteelID").toString();

                steel->setSteelName(steelID);
                QString type = query.record().value("SteelType").toString();

                steel->setSteelType(type);


                int w = query.record().value("Width").toInt();
                int t =query.record().value("Thick").toInt();
                int l =query.record().value("Length").toInt();
                int toplen = query.record().value("TopLen").toInt();


                steel->setLength(l);
                steel->setWidth(w);
                steel->setThick(t);
                steel->setTopLen(toplen);
                steel->setLevel(0);
            }
        emit SteelinfoBaseChanged(sequences,steel);
    }

}
